<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class BuysideService 
{
	
	public static function process_dadf_request($request) {
		
		$dadf_version = Zend_Registry::get('application.version');
		
	    try {		
    		$request_xml = $request->getParam('xml');
    		$transformer = new IncomingRequestTransformer($request_xml, 'Dadf.request-' . $dadf_version . '.xsd', 'Incoming');
    		$bid_request_factory = IncomingBidRequestFactory::get_instance();
    		$params["IncomingBidRequestID"] = $transformer->bid_request_id; 
    		$bid_request = $bid_request_factory->get_row($params);
    		$bid_request->load_children();
    		
    		RequestResponseService::process_dadf_request($bid_request);

    	} catch (Exception $e) {
    				
    		if (DEBUG === true):
    			echo 'Caught exception: ',  $e->getMessage(), "\n";
    		endif;
    		
    		echo "Bad Request Response";
    			
    	}
	}
	
	public static function process_dadf_accept($request) {
		
		$dadf_version = Zend_Registry::get('application.version');
        
		try {
    				
    		$request_xml = $request->getParam('xml');
    		$transformer = new IncomingAcceptTransformer($request_xml, 'Dadf.accept-' . $dadf_version . '.xsd', 'Incoming');
    		$accept_response_factory = IncomingAcceptResponseFactory::get_instance();

    		$accept_response_list = array();
    		
    		foreach ($transformer->accept_response_id_list as $accept_response_id):
    		
	    		$params["IncomingAcceptResponseID"] = $accept_response_id; 
	    		$accept_response = $accept_response_factory->get_row($params);
				$accept_response->load_children();
    			$accept_response_list[] = $accept_response;
    			
			endforeach;
    		
    		AcceptResponseService::process_dadf_accept($accept_response_list);
    		
    	} catch (Exception $e) {
    				
    		if (DEBUG === true):
    			echo 'Caught exception: ',  $e->getMessage(), "\n";
    		endif;
    		
    		echo "Accept Response";
    	}    
    	
    	
	}
	
}